var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var Item_Shop_BoxItem_UIBindings = require("AA_Item_Shop_BoxItem_UIBindings");
var ResKeeper = require("AA_ResKeeper");
var Excel = require("AA_Excel");
var HallManager = require("AA_HallManager");
var PlayerDataManager = require("AA_PlayerDataManager");
var EventManager = require("AA_EventManager");
var EvenType = require("AA_EvenType");
var Const_Hall = require("AA_Const_Hall");
var UIManager = require("AA_UIManager");
var BundleConfig = require("AA_BundleConfig");
var UIConfig_Home = require("AA_UIConfig_Home");
var HallGlobalVariable = require("AA_HallGlobalVariable");
var ADManager = require("AA_ADManager");
var CommonUtils = require("AA_CommonUtils");
var Const_Common = require("AA_Const_Common");
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;

var _def_Item_Shop_BoxItem = function (e) {
  function _creator() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.auto_ui = null;
    t.isStart = true;
    t.childIndex = 0;
    t.delateTime = 0;
    return t;
  }
  __extends(_creator, e);
  _creator.prototype.onLoad = function () {
    this.auto_ui.addClickListener(this._clickListener, this);
  };
  _creator.prototype.StartSet = function (e, t) {
    undefined === t && (t = null);
    t && (this.timeLabel = t);
    this.childIndex = e;
    var o = Excel.Excel.shopControl(1);
    if (this.isStart) {
      this.isStart = false;
      this.auto_ui.txt_nameLabel.string = 0 == e ? "普通礼盒" : "精致礼盒";
      this.auto_ui.txt_priceLabel.string = 0 == e ? "" + o.boxPrice : "" + o.bigBoxPrice;
      this.auto_ui.node_ptBg.active = 0 == e;
      this.auto_ui.node_jzBg.active = 0 != e;
    }
    var n = Excel.Excel.shopBoxLevel(HallManager.HallMgr.shopData.boxLevel + 1);
    this.rewards = 0 == e ? n.rewards : n.bigRewards;
    var i = cc.Color.GREEN;
    var a = "普通";
    if (this.rewards[2]) {
      i = new cc.Color(223, 84, 219, 255);
      a = "稀有";
    } else if (this.rewards[1]) {
      a = "高级";
      i = new cc.Color(12, 224, 255, 255);
    }
    this.auto_ui.txt_mustGetLabel.string = a;
    this.auto_ui.txt_mustGetLabel.node.color = i;
    this.auto_ui.btn_videoBuyBtn.interactable = 0 == e ? o.box1BuyCount - HallManager.HallMgr.shopData.box1BuyCount > 0 : o.box2BuyCount - HallManager.HallMgr.shopData.box2BuyCount > 0;
    this.auto_ui.txt_timeLabel.node.active = !this.auto_ui.btn_videoBuyBtn.interactable;
  };
  _creator.prototype._clickListener = function (e) {
    switch (e) {
      case this.auto_ui.btn_videoBuyBtn:
        this.VideoBuyBox(true, null);
        break;
      case this.auto_ui.btn_buyBtn:
        this.BuyThis(false);
        break;
      case this.auto_ui.btn_showInfoBtn:
        var t = new Const_Hall.BoxInfoData();
        t.type = this.childIndex;
        t.clickCallBack = this.VideoBuyBox.bind(this);
        UIManager.default.open(BundleConfig.BundleNames.Home, UIConfig_Home.UIView_Home.UI_BoxInfo, t);
    }
  };
  _creator.prototype.VideoBuyBox = function (e, t) {
    var o = this;
    if (e) {
      var n = "商城视频购买普通宝箱";
      0 == this.childIndex && (n = "商城视频购买精致宝箱");
      ADManager.ADMgr.showRewardedVideo(n, function () {
        o.BuyThis(true);
        t && t();
      });
    } else {
      this.BuyThis(false);
      t && t();
    }
  };
  _creator.prototype.BuyThis = function (e) {
    var t = Excel.Excel.shopControl(1);
    var o = 0 == this.childIndex ? t.boxPrice : t.bigBoxPrice;
    if (e) {
      if (0 == this.childIndex) {
        HallManager.HallMgr.shopData.box1BuyCount++;
      } else {
        HallManager.HallMgr.shopData.box2BuyCount++;
      }
      this.GetReward(o);
      this.StartSet(this.childIndex);
    } else if (PlayerDataManager.PlayerDataMgr.SubDiamond(o)) {
      var n = "商城钻石购买普通宝箱";
      0 == this.childIndex && (n = "商城钻石购买精致宝箱");
      CommonUtils.default.SendPoint(Const_Common.SendPointType.外围打点, n);
      this.GetReward(o);
      this.StartSet(this.childIndex);
    }
  };
  _creator.prototype.GetReward = function (e) {
    var t = this;
    var o = Excel.Excel.shopBoxLevel(HallManager.HallMgr.shopData.boxLevel + 1);
    var n = new Const_Hall.OpenBoxViewData();
    n.boxType = this.childIndex;
    n.boxCount = 0;
    var i;
    var a = 0;
    if (0 == this.childIndex) {
      a = o.moneyCount || 0;
      i = [o.rewards[0] || 0, o.rewards[1] || 0, o.rewards[2] || 0];
    } else {
      a = o.bigMoneyCount || 0;
      i = [o.bigRewards[0] || 0, o.bigRewards[1] || 0, o.bigRewards[2] || 0];
    }
    n.boxCount = i[0] + i[1] + i[2];
    n.clickCallBack = function () {
      var n = [[], [], []];
      Excel.Excel.shuiguo_all.forEach(function (e) {
        PlayerDataManager.PlayerDataMgr.GetEquipDataById(e.id).isUnLock && -1 != e.isShow && n[e.quality - 1].push(e);
      });
      var r = new Map();
      for (var c = 0; c < i.length; c++) {
        var s = i[c];
        if (0 != n[c].length) {
          for (var l = 0; l < s; l++) {
            var p = Math.floor(Math.random() * n[c].length);
            var h = n[c][p];
            r.has(h.id) || r.set(h.id, 0);
            var v = r.get(h.id);
            v++;
            r.set(h.id, v);
          }
        }
      }
      var b = new Const_Hall.GetItemsData();
      b.ids = [];
      b.counts = [];
      if (a > 0) {
        b.ids.push(HallGlobalVariable.default.goldId);
        b.counts.push(a);
      }
      r.forEach(function (e, t) {
        b.ids.push(t);
        b.counts.push(e);
      });
      b.clickCallBack = function () {
        for (var n = 0; n < b.ids.length; n++) {
          var i = b.ids[n];
          var a = b.counts[n];
          if (i == HallGlobalVariable.default.goldId) {
            PlayerDataManager.PlayerDataMgr.AddGold(a);
          } else {
            PlayerDataManager.PlayerDataMgr.AddEquipFragments(i, a);
          }
        }
        t.GetRewardEnd({
          needDiamond: e,
          nextExp: o.exp
        });
      };
      UIManager.default.open(BundleConfig.BundleNames.Home, UIConfig_Home.UIView_Home.UI_GetItems, b);
    };
    UIManager.default.open(BundleConfig.BundleNames.Home, UIConfig_Home.UIView_Home.UI_OpenBox, n);
  };
  _creator.prototype.GetRewardEnd = function (e) {
    var t = HallManager.HallMgr.shopData.boxLevel < Excel.Excel.shopBoxLevel_all.size - 1 && HallManager.HallMgr.shopData.boxExp + e.needDiamond >= e.nextExp;
    HallManager.HallMgr.shopData.boxExp += e.needDiamond;
    HallManager.HallMgr.SaveShopData();
    EventManager.EventMgr.dispatchEvent(EvenType.EVENT_TYPE.HallResetShopBox);
    t && UIManager.default.open(BundleConfig.BundleNames.Home, UIConfig_Home.UIView_Home.UI_BoxUpLevel);
  };
  _creator.prototype.update = function (e) {
    this.delateTime += e;
    if (this.delateTime > .2) {
      this.delateTime = 0;
      !this.auto_ui.btn_videoBuyBtn.interactable && this.timeLabel && (this.auto_ui.txt_timeLabel.string = this.timeLabel.string);
    }
  };
  __decorate([_property(Item_Shop_BoxItem_UIBindings.default)], _creator.prototype, "auto_ui", undefined);
  return __decorate([_ccclass], _creator);
}(ResKeeper.default);
exports.default = _def_Item_Shop_BoxItem;